﻿#region License

// Copyright © 2004 - 2010 All Right Reserved, Derek Goslin
// 
// 1. Definitions
// The terms "reproduce," "reproduction," "derivative works," and "distribution" have the same meaning here as under U.S. copyright law.
// A "contribution" is the original software, or any additions or changes to the software.
// A "contributor" is any person that distributes its contribution under this license.
// "Licensed patents" are a contributor's patent claims that read directly on its contribution.
// 
// 2. Grant of Rights
// (A) Copyright Grant- Subject to the terms of this license, including the license
//     conditions and limitations in section 3, each contributor grants you a non-exclusive,
//     worldwide, royalty-free copyright license to reproduce its contribution, prepare derivative 
//     works of its contribution, and distribute its contribution or any derivative works that you create.
// (B) Patent Grant- Subject to the terms of this license, including the license conditions and limitations
//     in section 3, each contributor grants you a non-exclusive, worldwide, royalty-free license under its
//     licensed patents to make, have made, use, sell, offer for sale, import, and/or otherwise dispose of
//     its contribution in the software or derivative works of the contribution in the software.
// 
// 3. Conditions and Limitations
// (A) Reciprocal Grants- For any file you distribute that contains code from the software (in source code or binary format), 
//     you must provide recipients the source code to that file along with a copy of this license, which license will govern that file. 
//     You may license other files that are entirely your own work and do not contain code from the software under any terms you choose.
// (B) No Trademark License- This license does not grant you rights to use any contributors' name, logo, or trademarks.
// (C) If you bring a patent claim against any contributor over patents that you claim are infringed by the software,
//     your patent license from such contributor to the software ends automatically.
// (D) If you distribute any portion of the software, you must retain all copyright, patent, trademark,
//     and attribution notices that are present in the software.
// (E) If you distribute any portion of the software in source code form, you may do so only under this license by including a complete copy 
//     of this license with your distribution. If you distribute any portion of the software in compiled or object code form, you may only do 
//     so under a license that complies with this license.
// (F) The software is licensed "as-is." You bear the risk of using it. The contributors give no express warranties, guarantees or conditions.
//    You may have additional consumer rights under your local laws which this license cannot change. To the extent permitted under your
//    local laws, the contributors exclude the implied warranties of merchantability, fitness for a particular purpose and non-infringement.

#endregion

using System;

namespace DnEcuDiag.Conversion
{
    public class M155Conversion : Convertor, IConvertor
    {
        private readonly float[] _m155TempTable;

        public M155Conversion()
        {
            _m155TempTable = new[]
                                 {
                                     -33f, -33f, -33f, -33f, -30.8f, -30.8f, -30.8f, -28.9f, -28.9f, -28.9f, -28.9f,
                                     -27f, -27f, -25.4f,
                                     -25.4f,
                                     -23.8f, -23.8f, -23.8f, -22.3f, -22.3f, -20.9f, -20.9f, -19.6f, -19.6f, -18.3f,
                                     -18.3f, -17.1f, -16f
                                     , -16f, -14.9f, -13.8f, -13.8f, -12.8f, 11.8f, 11.8f,
                                     -10.8f, -10.8f, -9.9f, -9.9f, -8.1f, -8.1f, -7.2f, -6.4f, -6.4f, -5.6f, -4.8f, -4f,
                                     -3.2f, -2.5f,
                                     -1.8f, -1f, -0.3f, 0.4f, 1f, 1.7f, 2.4f, 3f, 3.7f, 4.3f, 5.5f, 6.1f,
                                     6.7f, 7.3f, 7.9f, 8.5f, 9f, 9.6f, 10.2f, 10.7f, 11.3f, 11.8f, 12.3f, 13.4f, 13.9f,
                                     14.4f, 15.5f, 16f
                                     , 16.5f, 17.5f, 18f, 18.5f, 19.4f, 19.9f, 20.4f, 21.3f, 21.8f, 22.3f,
                                     23.2f, 23.7f, 24.6f, 25.1f, 25.5f, 26.4f, 26.9f, 27.4f, 28.3f, 28.7f, 29.2f, 30f,
                                     30.5f, 30.9f,
                                     31.8f, 32.3f, 33.2f, 33.6f, 34.5f, 34.9f, 35.4f, 36.2f, 36.7f, 37.6f,
                                     38f, 38.9f, 39.3f, 39.8f, 40.7f, 41.1f, 42f, 42.5f, 43.4f, 43.8f, 44.7f, 45.2f,
                                     45.6f, 46.5f, 47f,
                                     47.9f, 48.4f, 49.3f, 49.8f, 50.2f, 51.2f, 51.6f, 52.6f, 53.1f, 53.6f,
                                     54.4f, 55f, 55.5f, 56.5f, 57f, 57.5f, 58.5f, 59.1f, 59.6f, 60.6f, 61.2f, 61.7f,
                                     62.8f, 63.3f, 63.9f,
                                     65f, 65.5f, 66.1f, 67.2f, 67.8f, 68.4f, 69.6f, 70.2f, 70.8f, 70.8f,
                                     71.4f, 72.1f, 72.7f, 73.3f, 74f, 74.6f, 74.6f, 75.3f, 76f, 76.7f, 77.3f, 78f, 78.7f
                                     , 79.5f, 79.5f,
                                     80.2f, 80.9f, 81.7f, 81.7f, 82.4f, 83.2f, 84f, 84.8f, 85.6f, 86.4f, 86.4f,
                                     87.2f, 88.1f, 88.9f, 88.9f, 89.8f, 90.7f, 90.7f, 91.6f, 92.5f, 92.5f, 93.5f, 93.5f,
                                     94.5f, 95.5f,
                                     95.5f, 96.5f, 96.5f, 97.5f, 98.6f, 98.6f, 99.6f, 100.8f, 100.8f, 101.9f,
                                     101.9f, 103.1f, 104.3f, 104.3f, 105.5f, 106.8f, 106.8f, 108.1f, 108.1f, 108.1f,
                                     109.5f, 109.5f,
                                     110.9f, 110.9f, 110.9f, 112.3f, 112.3f, 112.3f, 113.8f, 113.8f, 115.3f,
                                     115.3f, 115.3f, 116.9f, 124f, 124f, 124f, 124f, 124f, 124f, 124f, 124f, 124f, 125f,
                                     125f, 125f, 125f
                                     , 125f, 125f, 125f, 125f, 125f, 125f, 125f, 125
                                 };
        }

        #region IConvertor Members

        public float Convert(int conversionId, byte[] rawData)
        {
            float result = 0;
            UInt32 rawValue = ConvertByteArray(rawData);

            switch (conversionId)
            {
                case 0:
                    result = rawValue;
                    break;
                case 1:
                    // Km conversion.
                    result = rawValue*0.2832f;
                    break;
                case 2:
                    // A function for RPM (M1.5.5)
                    result = rawValue*40;
                    break;
                case 3:
                    // Mass air flow.
                    result = rawValue*1.6f;
                    break;
                case 4:
                    // Advance angle
                    result = 78 - ((int) rawValue*.75f);
                    break;
                case 5:
                    // Injection time.
                    result = rawValue*1.519f;
                    break;
                case 6:
                    // Throttle valve angle.
                    result = rawValue*0.469f;
                    break;
                case 7:
                    result = rawValue*0.75f;
                    break;
                case 8:
                    // Filtered load.
                    result = rawValue*50;
                    break;
                case 9:
                    result = (rawValue + 251)*2.2297f;
                    break;
                case 10:
                    // Airflow in millivolts
                    result = rawValue*19.6f;
                    break;
                case 11:
                    // A function for battery (M1.5.5)
                    result = rawValue*.0723f;
                    break;
                case 12:
                    result = rawValue*1.2f;
                    break;
                case 13:
                    // CHECK
                    //result = ((int)rawValue * 4) - 267;
                    result = (-54.585f + rawValue)/204;
                    break;
                case 14:
                    // Target idle speed.
                    // RPM high resolution.
                    result = rawValue*10;
                    break;
                case 15:
                    result = (rawValue*80.0916f)/32767;
                    break;
                case 16:
                    // Duty Cycle canister
                    result = rawValue*.39f;
                    break;
                case 17:
                    // TPS Absolute Output.
                    result = rawValue*0.469f;
                    break;
                case 18:
                    result = _m155TempTable[rawValue];
                    break;
                case 19:
                    // Secondary load.
                    result = rawValue*48.61f;
                    break;
                case 20:
                    // Injection Time Correction
                    result = rawValue*.01f;
                    break;
                case 21:
                    result = rawValue*.02f;
                    break;
                case 22:
                    // Lambda Regulation Row 1
                    result = rawValue/128f;
                    break;
                case 23:
                    // Addition factor Row 1
                    result = (((int) rawValue - 128)*1280)*3.04f;
                    break;
                case 24:
                    result = rawValue*.39f;
                    break;
                case 25:
                    result = rawValue*1.159f;
                    break;
            }

            return result;
        }

        #endregion
    }
}